package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aixingfu.sports.Adapter.PrivateEducationAdapter;
import com.aixingfu.sports.MVP.contract.PersonalTrainContract;
import com.aixingfu.sports.MVP.presenter.PersonalTrainPresenter;
import com.aixingfu.sports.Model.PersonalTrainModel;
import com.aixingfu.sports.R;
import com.aixingfu.sports.bean.MessageEventPrivateEducation;
import com.aixingfu.sports.view.SpaceItemDecoration;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orie.librarybase.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
//选择私教课
@Route(path = "/main/PrivateEducationActivity")
public class PrivateEducationActivity extends BaseActivity implements PersonalTrainContract.MyView {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycle_private_education)
    RecyclerView recyclePrivateEducation;
    private PrivateEducationAdapter privateEducationAdapter;
    private List<PersonalTrainModel.DataBean.PrivateClassBean> privateClassBeanList = new ArrayList<>();
    private PersonalTrainPresenter personalTrainPresenter;
    private String member_id;
    private List<PersonalTrainModel.DataBean.PrivateClassBean> private_class = new ArrayList<>();
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_private_education;
    }
    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/PrivateEducationActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setText("选择私教课");
        Bundle bundle = getIntent().getExtras();
        member_id = bundle.getString("member_id", "");
        personalTrainPresenter = new PersonalTrainPresenter();
        personalTrainPresenter.attachView(this);
        getPrivateEducation();

        recyclePrivateEducation.setLayoutManager(new LinearLayoutManager(PrivateEducationActivity.this, LinearLayoutManager.VERTICAL, false));
        privateEducationAdapter = new PrivateEducationAdapter(PrivateEducationActivity.this, R.layout.item_private_education, privateClassBeanList);
        recyclePrivateEducation.addItemDecoration(new SpaceItemDecoration(0, 20));
//        //设置分割线
//        recycleList.addItemDecoration(new DividerItemDecoration(
//                getActivity(), DividerItemDecoration.VERTICAL));
        recyclePrivateEducation.setAdapter(privateEducationAdapter);
        privateEducationAdapter.setEmptyView(LayoutInflater.from(PrivateEducationActivity.this).inflate(R.layout.empty_data, null, false));
        privateEducationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                // 发布事件
                EventBus.getDefault().post(new MessageEventPrivateEducation(privateClassBeanList.get(position).getName(),privateClassBeanList.get(position).getDetail().get(0).getCourse_name(),privateClassBeanList.get(position).getDetail().get(0).getOriginal_price(),privateClassBeanList.get(position).getDetail().get(0).getCourse_num(),privateClassBeanList.get(position).getId()));
                finish();
            }
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void beforeInit() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showToast(String msg, String state) {

    }
    public void getPrivateEducation(){
        if (personalTrainPresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("member_id",member_id);
            personalTrainPresenter.getPersonalTrain(map);
        }
    }
    @Override
    public void setPersonalTrain(PersonalTrainModel data) {
    if (data.getCode() == 1){
        private_class = data.getData().getPrivate_class();
        privateClassBeanList.addAll(private_class);
        privateEducationAdapter.setNewData(privateClassBeanList);

    }
    }

    @Override
    public void showLoading() {
showLoadingDialog();
    }

    @Override
    public void hideLoading() {
dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }
}
